package com.crm.im.core.handler;

import java.util.Locale;
 
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

/**
 *返回websocket安全验证
 * 
 * @author
 */
public class WebSocketHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
	   
       private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);
	   
       @Override
       protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
	           // ping and pong frames already handled
   
	          if (frame instanceof TextWebSocketFrame) {
               // Send the uppercase string back.
              String request = ((TextWebSocketFrame) frame).text();
              logger.info("{} received {}", ctx.channel(), request);
	              ctx.channel().writeAndFlush(new TextWebSocketFrame(request.toUpperCase(Locale.CHINA)));
         } else {
              String message = "unsupported frame type: " + frame.getClass().getName();
               throw new UnsupportedOperationException(message);
          }
      }
   }